import requests
from bs4 import BeautifulSoup


# for i in range(0, 11):
url = 'https://movie.douban.com/top250'
    # url = url + str(i * 25)
    # print(url)
h = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
r = requests.get(url, headers=h)
with open('douban.html', 'w', encoding='utf-8')as f:
    f.write(r.content.decode('utf-8'))
soup = BeautifulSoup(r.content.decode('utf-8'), 'lxml')
r1 = soup.find_all('div', {'class': 'info'})
# print(r1)
head = []
for e in r1:
    h = {}
    h['电影'] = e.find('span', {'class': 'title'}).string
    h['评分'] = e.find('span', {'class', 'rating_num'}).string
    h['链接'] = e.find('a')['href']
    # print(h)
    head.append(h)
with open('douban.txt','w',encoding='utf8')as f:
    f.write(head.__str__())
